﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Ext.Net;
using System.Collections.Generic;
using Sio.Mdm.Client;
using Sio.Mdm.BF.Sifranti;
using Sio.Mdm.Entities;
using Sio.Mdm.Types.Database;
using Sio.Mdm.BF.Administracija;
using Sio.Mdm.BF.Idm;
using Sio.Mdm.Web.Controls;

namespace Sio.Mdm.Web.Organizacija
{
  public partial class NovUceci : Sio.Mdm.Web.Controls.Page
  {
	protected void Page_Load(object sender, EventArgs e)
	{
	  if (!IsPostBack && !X.IsAjaxRequest)
	  {
		//pošte
		EntityCollection<PostnaStevilka> poste = new EntityCollection<PostnaStevilka>(
			new Filter<PostnaStevilka>().Where(o => o.PostaId != null).OrderBy(o => o.PostnaStevilka));
		this.StorePoste.DataSource = poste;
		this.StorePoste.DataBind();

		//šolskaLeta
		EntityCollection<SolskoLeto> solskaLeta = new EntityCollection<SolskoLeto>(
			new Filter<SolskoLeto>().Where(o => o.LetoId != null).OrderByDescending(o => o.KonecLeta));
		this.StoreSolskaLeta.DataSource = solskaLeta;
		this.StoreSolskaLeta.DataBind();

		//privzete izbire
		int letoId = Request.GetValue<int>("letoid");
		int razredId = Request.GetValue<int>("razredid");

		if (letoId > 0)
		{
		  this.cbSolskoLeto.SelectedItem.Value = letoId.ToString();
		  StoreRazrediRefresh(sender, null);
		  if (razredId > 0)
			this.cbRazredi.SelectedItem.Value = razredId.ToString();
		}
	  }
	}
	protected void StoreRazrediRefresh(object sender, StoreRefreshDataEventArgs e)
	{
	  int letoId = -1;
	  if (cbSolskoLeto.SelectedItem.Value != "")
		letoId = Convert.ToInt16(cbSolskoLeto.SelectedItem.Value);

	  //razredi
	  EntityCollection<RazredSkupina> razredi = new EntityCollection<RazredSkupina>(
							  new Filter<RazredSkupina>().Where(r => r.LetoId == letoId).OrderBy(r => r.Razred).OrderBy(r => r.Paralelka));
	  this.StoreRazredi.DataSource = razredi;
	  this.StoreRazredi.DataBind();
	}

	protected void AddUceciButton(object sender, DirectEventArgs e)
	{
	  Sio.Mdm.Web.Controls.DialogButtons button =
		  e.DeserializeParameter<Sio.Mdm.Web.Controls.DialogButtons>("buttonType");

	  if (button == DialogButtons.Save)
	  {
		//AddUceciProxy.NotifyParent(1, 2, "Hello");
		//AddUceciProxy.DisableButton(DialogButtons.Save);
		//List<string> napake = new List<string>();

		//if (String.IsNullOrEmpty(this.inputIme.Text) | (this.inputIme.Text.Length <= 2))
		//  napake.Add(" - ime");
		//if (String.IsNullOrEmpty(this.inputPriimek.Text) | (this.inputPriimek.Text.Length <= 2))
		//  napake.Add(" - priimek");
		//if (String.IsNullOrEmpty(this.inputNaslov.Text) | (this.inputNaslov.Text.Length <= 2))
		//  napake.Add(" - naslov");
		//if (String.IsNullOrEmpty(this.inputEmso.Text) | (this.inputEmso.Text.Length <= 2))
		//  napake.Add(" - EMŠO");
		//if (String.IsNullOrEmpty(this.inputDs.Text) | (this.inputDs.Text.Length <= 2))
		//  napake.Add(" - davčna številka");
		//if (napake.Count > 0)
		//{
		//  e.Success = false;
		//  X.Msg.Alert("Manjkajoči podatki", "Vpisati morate še naslednja vnosna polja: <br />" + String.Join("<br />", napake.ToArray())).Show();
		//  return;
		//}
		//else
		//{
		  Ucec ucec = EntityFactory.CreateInstance<Ucec>();
		  ucec.Ime = this.inputIme.Value.ToString();
		  ucec.Priimek = this.inputPriimek.Value.ToString();
		  ucec.Naslov = this.inputNaslov.Value.ToString();
		  ucec.Emso = Convert.ToInt64(this.inputEmso.Value);
		  ucec.DavcnaStevilka = Convert.ToInt32(this.inputDs.Value);
		  ucec.Spol = Convert.ToInt32(this.cbSpol.SelectedItem.Value);
		  if (this.cbPoste.SelectedItem.Value != "")
			ucec.PostaId = Convert.ToInt32(this.cbPoste.SelectedItem.Value);
		  //obvezen dodatek za trigger
		  ucec.OrganizacijaId = this.UserSession.Organizacija;
		  ucec.TipVlogeId = 3;

		  var slService = ServiceFactory.GetService<IUceciService>();
		  slService.Create(ucec, this.UserSession);
		  if (this.cbRazredi.SelectedItem.Value != "")
		  {
			UcecStatus ucecStatus = EntityFactory.CreateInstance<UcecStatus>();
			ucecStatus.VlogaId = ucec.VlogaId;
			int idm = ucec.IdmStevilka;
			ucecStatus.RazredId = Convert.ToInt32(this.cbRazredi.SelectedItem.Value);
			ucecStatus.LetoId = Convert.ToInt32(this.cbSolskoLeto.SelectedItem.Value);
			var slServiceUcecStatus = ServiceFactory.GetService<IUceciStatusiService>();
			slServiceUcecStatus.Create(ucecStatus, this.UserSession);
			idm = ucecStatus.UceciStatusID;
		  }
		  AddUceciProxy.Close();
		//}
	  }
	  if (button == Sio.Mdm.Web.Controls.DialogButtons.Close)
	  {
		AddUceciProxy.Close();
	  }
	}

  }
}
